<?php

define( 'data_path', rootpath . 'grabber/data/');

include_once( rootpath . 'Smarty.class.php');
$main_smarty = new Smarty;

include( rootpath . 'config.php');
include(mnminclude.'html1.php');
include(mnminclude.'link.php');
include(mnminclude.'tags.php');
include(mnminclude.'search.php');
include(mnminclude.'smartyvariables.php');
require_once( rootpath . 'grabber/core/translate/GTranslate.php');

require_once( rootpath . 'grabber/core/Proxy.php');
require_once( rootpath . 'grabber/core/Browser.php');
require_once( rootpath . 'grabber/core/model/Abstract.php');

require_once( rootpath . 'grabber/core/model/Parser.php');
require_once( rootpath . 'grabber/core/model/Parser/News.php');
require_once( rootpath . 'grabber/core/model/Parser/Comment.php');

require_once rootpath . 'grabber/grabbers/abstract.php';

require_once( rootpath . 'grabber/core/model/users.php');
require_once( rootpath . 'grabber/core/model/links.php');
require_once( rootpath . 'grabber/core/model/comments.php');
require_once( rootpath . 'grabber/core/model/file.php');


class Grabber_Application
{
	
	protected $_db = null;


	protected $_oProxy = null;
	protected $_oBrowser = null;
	protected $_mNews = null;
	protected $_mParser = null;
	protected $_mComment = null;
	
	protected $_oParser = null;


	public function __construct( $db )
	{
		$this->_db = $db;
		
		$this->_oProxy   = new Proxy();
		$this->_oBrowser = new Browser();

		$this->_mNews	  = new Parser_News( $this->_db );
		$this->_mParser  = new Parser( $this->_db );
		$this->_mComment = new Parser_Comment( $this->_db );
	}
	
	public function run( $_request )
	{
		$this->_callOperation( $_request );
	}
	
	protected function _callOperation( $_request )
	{
		if( $_request['install'] == 1 && $_request['user'] == 'admin' && $_request['pass'] == 'nhfnfnf' )
		{
			require_once rootpath . 'grabber/install.php';
			return true;
		}
		
		$parser_row = $this->_mParser->getParserForUpdate( date('Y-m-d', time()) );
		$this->_oParser = $this->_loadParser( $parser_row );
		
		if( $this->_callFunc( $this->_oParser, $_request ) )
			return true;

		$parser_row = $this->_mParser->getParserForUpdate( );
		$this->_oParser = $this->_loadParser( $parser_row );
		
		if( $this->_callFunc( $this->_oParser, $_request ) )
			return true;
	}
	
	protected function _callFunc( $obj, $_request )
	{
		if( !$obj || empty( $_request['operation'] ) )
			return false;
		
		$method = $_request['operation'];
		$method[0] = mb_strtoupper( $method[0] );
		$method = 'parsing' . $method;
		
		if( method_exists( $obj , $method ) )
		{
			call_user_method( $method, $obj );
		}
		
		return true;
	}

	protected function _loadParser( $parser_row )
	{
		if( !empty( $parser_row ) && $parser_row->parser_name )
		{
			$class_name = str_replace('.', '_', $parser_row->parser_name);
			if( !empty( $class_name[0] ) )
			{
				$class_name[0] = strtoupper( $class_name[0] );
			}

			require_once rootpath . 'grabber/grabbers/'. $parser_row->parser_name .'.php';

			return new $class_name( $this->_db, $this->_mParser, $this->_mNews, $this->_mComment, $this->_oBrowser );
		}
		
		return null;
	}
	
}



/*

switch( true )
{
    case ($_REQUEST['install'] == 1 && $_REQUEST['user'] == 'admin' && $_REQUEST['pass'] == 'nhfnfnf'):
		require_once rootpath . 'grabber/install.php';
		break;

	case ($_REQUEST['type'] == 'parsing-news'):
		
		$new_for_parsing = $oNews->getNewsForParsing();
		
		foreach( $new_for_parsing as $key => $news )
		{
			$parser_row = $oParser->get( array('id = ' => $news->parser_id) );
			
			
			
			print_r($parser_row); exit;
			
			
			
//			$html = $oBrowser->get( $news->url );
//			
//			echo $html; exit;
//			
//			echo $news->url;
			
		}
		
//		print_r( $new_for_parsing ); 
		exit;
		
		break;
    case true:
		
		$parser_row = $oParser->getParserForUpdate();
		
		if( !empty( $parser_row ) && $parser_row->parser_name )
		{
			$class_name = str_replace('.', '_', $parser_row->parser_name);
			if( !empty( $class_name[0] ) )
			{
				$class_name[0] = strtoupper( $class_name[0] );
			}

			require_once rootpath . 'grabber/grabbers/'. $parser_row->parser_name .'.php';

			$parser = new $class_name( $this->_db, $oParser, $oNews, $oComment, $oBrowser );
		}
		
		
		
		
		
		
//        $gt = new GTranslate();
//        
//        $ts = "";
//        
//        $result = $gt->russian_to_english($ts);
//		
//		print_r( $result ); exit();
        
        
	
//	$oParser = new Parser( $this->_db );
//	$id = $oParser->insert('hello', 'http://google.com');
//	$oParser->update($id, 'hello2');
//
//	$oNews = new Parser_News( $this->_db );
//	echo $news_id = $oNews->insert($id, 21, 'Very important news', 'http://github.ru/');
//	$oNews->update($news_id, $id, 21, 'Very not important news )', 'http://github.ru/');
//	
//	$oComment = new Parser_Comment( $this->_db );
//	echo $comment_id = $oComment->insert($id, $news_id, 45, 'Very important comment');
//	$oComment->update($comment_id, $id, $news_id, 45, 'Very not important comment )');
	
	
	
	
	
	
	
	
	
	
//	require_once rootpath . 'grabber/grabbers/'. $_REQUEST['grabber'] .'.php';
	break;
}*/